<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>肌·密礼盒</title>
    <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
    <style>
        html,
        body,
        ul,
        li,
        p {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        img,
        canvas {
            vertical-align: middle;
        }

        .bgimg {
            width: 100%;
            height: 100%;
        }

        #app {
            max-width: 100vw;
            height: 100vh;
        }

        [class^='page'] {
            position: relative;
        }

        .page0 .toPage1 {
            position: absolute;
            top: 75.7%;
            left: 23%;
            display: block;
            width: 53.33%;
            height: 3.33%;
        }

        .page1 .uploadImg {
            position: absolute;
            width: 64%;
            height: 37%;
            top: 36%;
            left: 19%;
            background-size: cover;
            background-repeat: no-repeat;
            background-position: 50% 50%;
        }

        .page1 .uploadImg input {
            width: 100%;
            height: 100%;
            opacity: 0;
            outline: none;
        }

        .toPage2 {
            position: absolute;
            width: 53%;
            height: 6.3%;
            top: 75.7%;
            left: 23.3%;
            border-radius: 1rem;
        }

        .toPop1 {
            position: absolute;
            width: 33%;
            height: 3.3%;
            top: 83.7%;
            left: 33.3%;
        }

        .pop1_box {
            position: absolute;
            top: 0;
            background: rgba(36, 60, 70, 0.96);
        }

        .pop1_box .conversation_box {
            position: absolute;
            width: 81%;
            height: 63.7%;
            top: 15.8%;
            left: 10%;
            overflow-x: hidden;
            overflow-y: auto;
            -webkit-overflow-scrolling: touch;
            padding: 10px 25px;
            box-sizing: border-box;
        }

        .pop1_box .conversation {
            display: flex;
            margin-bottom: 10px;
        }

        .pop1_box .conversation.me {
            justify-content: flex-start;
            flex-direction: row-reverse;
        }

        .pop1_box .conversation .avatar {
            width: 37px;
            height: 37px;
        }

        .pop1_box .conversation .avatar img {
            width: 100%;
            height: 100%;
        }

        .pop1_box .conversation .content {
            position: relative;
            margin-left: 10px;
            background: #0ae7e4;
            border-radius: 5px;
            font-size: 14px;
            padding: 8px 10px;
            box-sizing: border-box;
            height: min-content;
        }

        .pop1_box .conversation.me .content {
            margin-right: 10px;
            margin-left: unset;
            background-image: -webkit-linear-gradient(0deg, #02e0f9, #24fda1);
            background-image: linear-gradient(90deg, #02e0f9, #24fda1);
        }

        .pop1_box .conversation.other .content::after {
            content: '';
            position: absolute;
            top: 10px;
            left: -5px;
            border-top: 5px solid transparent;
            border-right: 5px solid #0ae7e4;
            border-bottom: 5px solid transparent;
        }

        .pop1_box .conversation.me .content::after {
            content: '';
            position: absolute;
            top: 10px;
            right: -5px;
            border-top: 5px solid transparent;
            border-left: 5px solid #24fda1;
            border-bottom: 5px solid transparent;
        }

        .pop1_box .conversation .content li {
            margin: 4px 0;
        }

        .pop1_box .conversation .content span {
            background: #fff;
            padding: 0 5px;
        }

        .page2 .toPop2 {
            position: absolute;
            width: 33%;
            height: 3.3%;
            top: 85%;
            left: 33.3%;
        }

        .page2 .xiaohe_loading,
        .page2 .result_pop {
            position: absolute;
            top: 0;
            width: 100%;
            height: 100%;
            background: rgba(36, 60, 70, 0.96);
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .page2 .xiaohe_loading img {
            width: 80%;
        }

        .page2 .result_pop img {
            width: 90%;
        }

        .page2 .toPage3 {
            position: absolute;
            width: 33%;
            height: 4%;
            top: 58%;
            left: 33.3%;
        }

        .page3 .again {
            position: absolute;
            width: 33%;
            height: 3%;
            top: 92%;
            left: 53.7%;
        }

        .page3 .renderposter {
            position: absolute;
            width: 33%;
            height: 3%;
            top: 92%;
            left: 11.7%;
        }

        #cvs {
            /* width: 100%; */
            display: none;
        }

        .animat {
            position: absolute;
            top: 0;
            left: 0;
        }

        .wave {}

        .circle {}

        .light {}

        .title {}

        .topdot {}

        svg {
            vertical-align: middle;
        }

        .book {
            position: absolute;
            width: 46%;
            height: 0.7%;
            top: 8.8%;
            left: 27%;
            background-color: rgba(0, 0, 70, 0);
            border-radius: 1rem;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="page0" v-if="page === 0">
            <img class="bgimg" src="./img/机密礼盒详情页-.jpg">
            <a class="book" @click.prevent="toBook"></a>
        </div>
        <div class="page1" v-else-if="page === 1">
            <img class="bgimg" src="./img/恭喜你预约成功.jpg">
            <a class="toPage2" @click.prevent="toLottery"></a>
        </div>
        <div class="page2" v-else-if="page === 2">
            <img class="bgimg" src="./img/预约失败.jpg">
            <a class="toPop1" @click.prevent="toPop1"></a>
        </div>
        <div class="page3" v-else-if="page === 3" ref="poster">
            <img class="bgimg" ref="posterImg">
            <canvas id="cvs" ref="cvs"></canvas>
            <a class="again" @click.prevent="again"></a>
            <a class="renderposter" @click.prevent="renderposter"></a>
        </div>
    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                page: 0,
                imgUrl: '',
                imgId: '',
                showPop1: false,
                showLoading: false,
                showResult: false,
                isRegister: false,
                isBuyMeet: false
            },
            created: function () {
                //TODO API
                // 1.进入页面判断是否注册禾粉圈
                this.getUserInfo()

            },
            methods: {
                getUserInfo: function () {
                    var _this = this
                    $.ajax({
                        type: 'GET',
                        url: 'data.json',
                        data: {},
                        success: function (res) {
                            if (typeof (res) !== 'object') {
                                res = JSON.parse(res)
                            }
                            _this._data.isRegister = res.data[0].isRegister
                            _this._data.isBuyMeet = res.data[1].isBuyMeet

                            console.log(_this._data)
                            console.log(res)
                        },
                        error: function (err) {
                            console.log(err)
                        }
                    })
                },
                toPage1: function () {
                    this.page = 1
                },
                toBook: function () {
                    // 1.1判断是否注册
                    var _this = this
                    if (!_this.isRegister) {
                        // 1.2未注册重定向到禾粉圈
                        window.location.href = "https://www.google.com"
                    } else if (_this.isBuyMeet) {
                        // 1.3满足购买条件，预约成功，抽奖
                        this.page = 1
                    } else {
                        // 1.4 预约失败
                        this.page = 2
                    }
                },
                // 去禾粉圈抽奖
                toLottery: function () {
                    console.log('去禾粉圈抽奖')
                },
                upImg: function () {
                    var _this = this
                    var formData = new FormData()
                    formData.append('file', this.$refs.inpFile.files[0])
                    formData.append('key', 'Gn1xVdBiTClSSHKZifg0pTQSU5XGagWO')
                    $.ajax({
                        url: 'http://ydnshop.hepulanerp.com/sucai_t/public/index.php/post/post/uploadFile',
                        type: 'POST',
                        processData: false,
                        contentType: false,
                        dataType: "json",
                        data: formData,
                        success: function (res) {
                            console.log(res)
                            if (res.status === 200 && res) {
                                _this.imgUrl = res.info.url
                                _this.imgId = res.info.id
                            }
                        }
                    })
                },
                toPop2: function () {
                    this.showPop1 = false
                    this.showLoading = true
                    var _this = this
                    let answer = _this.renderQs.map(item => item.answer)
                    console.log(answer)
                    console.log(_this.imgUrl)
                    $.ajax({
                        url: 'http://hfc.suoluomei.com/hpl/index.php?s=/Weixin/questionnaire/setMessage',
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            imgUrl: _this.imgUrl,
                            answer: answer
                        },
                        success: function (res) {
                            console.log(res)
                            _this.showLoading = false
                        },
                        error: function (err) {
                            console.log(err)
                        }
                    })
                    setTimeout(function () {
                        _this.showResult = true
                    }, 2000)
                },
                toPage3: function () {
                    this.page = 3
                    this.$nextTick(function () {
                        this.initCanva()
                    })
                },
                again: function () {
                    window.location.reload()
                },
                renderposter: function () {
                    // console.log(this.$refs.poster)
                    // var _this = this
                    // html2canvas(this.$refs.poster, {
                    //   allowTaint: true,
                    //   useCORS: true,
                    //   scale: 3
                    // }).then(function (canvas) {
                    //   _this.page = -1
                    //   document.getElementById('app').appendChild(canvas)
                    //   alert('生成成功，请长按保存。')
                    // })
                    alert('生成成功，请长按保存。')
                },
                /*
                ctx:context
                str:要绘制的字符串
                X:绘制字符串起始x坐标
                Y:绘制字符串起始y坐标
                width: 绘制宽度
                lineHeight:字行高，自己定义个值即可
                */
                canvasTextAutoLine: function (ctx, str = '', X = 0, Y = 0, width = 300, lineHeight) {
                    var j = 0
                    var arrStr = []
                    lineHeight = lineHeight || ctx.font.split('px')[0]
                    for (var i = 0; i < str.length; i++) {
                        if (ctx.measureText(str.slice(j, i)).width >= width) {
                            arrStr.push(str.slice(j, i))
                            j = i
                        }
                    }
                    arrStr.push(str.slice(j))
                    arrStr.map(function (value, index) {
                        ctx.fillText(value, X, Y + index * lineHeight)
                    })
                },
                initCanva: function () {
                    var _this = this
                    var cvs = this.$refs.cvs
                    var ctx = cvs.getContext('2d')
                    var img = new Image()
                    img.src = './H5/04.png'
                    img.onload = function (e) {
                        var ratio = this.width / window.innerWidth
                        cvs.width = this.width
                        cvs.height = this.height

                        var lineheight = 45
                        var fonts = [{
                                c: '23岁',
                                x: 610,
                                y: 410
                            },
                            {
                                c: 'wow~你拥有自然的肤色，给人天然健康的感觉。同时，Q弹的脸颊有着满满的胶原蛋白呢！脸颊还有点点充满国际贵族气息的小雀斑，简直是小禾的梦中情Girl了~',
                                x: 170,
                                y: 580,
                                width: 500
                            }
                        ]

                        var font1 = ['现在你的肌肤已处于18~25岁', '{禾·防御}阶段满满的生命力~',
                            '但抗氧化工作也要提前做好哦！', '平日注意避免熬夜，做好防晒措施',
                            '保护皮肤不受太阳、风和寒冷天气的伤害', '坚持卸妆是个好习惯',
                            '但注意卸妆产品的选择，防止过度清洁'
                        ]

                        font1.forEach(function (value, index) {
                            fonts.push({
                                c: value,
                                x: 50,
                                y: 1000 + index * 55,
                            })
                        })

                        ctx.drawImage(this, 0, 0, cvs.width, cvs.height)

                        var grd = ctx.createLinearGradient(0, 0, cvs.width, 0)
                        grd.addColorStop(0.0, "#02e0f9")
                        grd.addColorStop(1.0, "#24fda1")

                        ctx.save()
                        ctx.fillStyle = grd
                        ctx.textAlign = 'left'
                        ctx.textBaseline = 'top'
                        ctx.font = 32 + 'px serif'
                        fonts.forEach(function (i) {
                            _this.canvasTextAutoLine(ctx, i.c, i.x, i.y, i.width || Infinity, i
                                .lineheight || lineheight)
                        })
                        ctx.restore()

                        _this.$refs.posterImg.src = cvs.toDataURL("image/png")
                    }
                }

            },
            mounted: function () {}
        })
    </script>
</body>

</html>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?189b36e08b329cba98211fa94afc844b";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>